/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUI.java
 *
 * Created on 6 mai 2010, 08:25:39
 */

package coursevoitures;

import java.util.Vector;
import tools.CourseVoituresException;

/**
 * L'interface Homme Machine de l'application.
 * @author Aurelien
 */
public class GUI extends javax.swing.JFrame {

    private Ctrl ctrl;

    private Vector nxtsAssocies, wiimotesAssociees;


    /** Creates new form GUI */
    public GUI(Ctrl c) {
        ctrl = c;

        nxtsAssocies = new Vector();
        wiimotesAssociees = new Vector();

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbConnecterWiimote = new javax.swing.JButton();
        jbListerNXT = new javax.swing.JButton();
        jbAssocierWiimoteNXT = new javax.swing.JButton();
        jbDeconnecterWiimote = new javax.swing.JButton();
        listeWiimote = new java.awt.List();
        listeNXT = new java.awt.List();
        jLabel1 = new javax.swing.JLabel();
        jlblInfoSysteme = new javax.swing.JLabel();
        listWiimoteNXT = new java.awt.List();
        jtfAdresseNXT = new javax.swing.JTextField();
        jbAssocierParAdresse = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtfNomNXT = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jbConnecterWiimote.setText("Connecter Wiimote");
        jbConnecterWiimote.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConnecterWiimoteActionPerformed(evt);
            }
        });

        jbListerNXT.setText("Lister NXT");
        jbListerNXT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbListerNXTActionPerformed(evt);
            }
        });

        jbAssocierWiimoteNXT.setText("Associer Wiimote/NXT");
        jbAssocierWiimoteNXT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAssocierWiimoteNXTActionPerformed(evt);
            }
        });

        jbDeconnecterWiimote.setText("Deconnecter Wiimote");
        jbDeconnecterWiimote.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDeconnecterWiimoteActionPerformed(evt);
            }
        });

        jLabel1.setText("Systeme:");

        listWiimoteNXT.setEnabled(false);

        jtfAdresseNXT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfAdresseNXTActionPerformed(evt);
            }
        });

        jbAssocierParAdresse.setText("Associer avec adresse");
        jbAssocierParAdresse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAssocierParAdresseActionPerformed(evt);
            }
        });

        jLabel2.setText("Adresse du NXT à associer:");

        jLabel3.setText("Nom du NXT à associer:");

        jMenu1.setText("Fichier");

        jMenuItem1.setText("Quitter");
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlblInfoSysteme, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbConnecterWiimote, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(listeWiimote, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfNomNXT, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                            .addComponent(jbListerNXT, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                            .addComponent(jtfAdresseNXT, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                            .addComponent(listeNXT, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(listWiimoteNXT, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jbDeconnecterWiimote, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jbAssocierWiimoteNXT, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jbAssocierParAdresse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbConnecterWiimote)
                    .addComponent(jbListerNXT)
                    .addComponent(jbAssocierWiimoteNXT))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(listeNXT, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .addComponent(listeWiimote, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbDeconnecterWiimote)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(listWiimoteNXT, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfNomNXT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfAdresseNXT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbAssocierParAdresse)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jlblInfoSysteme, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Methode appelee lors du clic sur le bouton "Connecter Wiimote".
     * @param evt
     */
    private void jbConnecterWiimoteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbConnecterWiimoteActionPerformed

        int[] id = ctrl.connecter_wiimote();

        boolean flag = false;

        if (id[0]==CourseVoituresException.AUCUNE_WIIMOTE){
            jlblInfoSysteme.setText("Aucune Wiimote a proximite");
        }
        else if(id[0] == CourseVoituresException.AUCUNE_NOUVELLE_WIIMOTE){
            jlblInfoSysteme.setText("Aucune nouvelle Wiimote a proximite");
        }
        else{
            for(int j = 0; j<id.length; j++){
                for (int i = 0; i < listeWiimote.getItems().length; i++) {
                    if (listeWiimote.getItems()[i].equals("" + id[j])) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    listeWiimote.add("" + id[j]);
                }
            }
        }
    }//GEN-LAST:event_jbConnecterWiimoteActionPerformed

    private void jbListerNXTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbListerNXTActionPerformed
        // TODO add your handling code here:
        String nomsRobots[];

        listeNXT.removeAll();

        jlblInfoSysteme.setText("Recherche en cours, veuillez patienter ...");
        nomsRobots = ctrl.rechercher_NXT();
        
        if (nomsRobots == (null) || nomsRobots.length == 0) {
            //System.out.println("Aucun NXT a proximite");
            //listeNXT.add("Aucun NXT a proximite");
            jlblInfoSysteme.setText("Aucun NXT a proximite");
        } else {
            for (int i = 0; i < nomsRobots.length; i++) {
                //System.out.println(i + ". " + nomsRobots[i]);
                listeNXT.add(nomsRobots[i]);
            }
        }
    }//GEN-LAST:event_jbListerNXTActionPerformed

    private void jbAssocierWiimoteNXTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAssocierWiimoteNXTActionPerformed
        // TODO add your handling code here:
        int idWiimote;
        String nomNXT;
        boolean resAssociation;

        if(listeWiimote.getSelectedItem() != null && listeNXT.getSelectedItem() != null){
            idWiimote = Integer.parseInt(listeWiimote.getSelectedItem());
            nomNXT = listeNXT.getSelectedItem();

            if(wiimotesAssociees.contains(idWiimote)){
                javax.swing.JOptionPane.showMessageDialog(null, "Cette Wiimote est déjà associée.");
            }else if(nxtsAssocies.contains(nomNXT)){
                javax.swing.JOptionPane.showMessageDialog(null, "Ce NXT est déjà associé.");
            }else{
                resAssociation = ctrl.associer_Wiimote_NXT(idWiimote, nomNXT);

                if (resAssociation) {
                    listWiimoteNXT.add(idWiimote + " - " + nomNXT);

                    /* Stockage des identifiants de Wiimote et NXT associes */
                    nxtsAssocies.add(nomNXT);
                    wiimotesAssociees.add(""+idWiimote);
                } else {
                    jlblInfoSysteme.setText("Probleme lors de l'association : " + idWiimote + " - " + nomNXT);
                }
            }
        }else{
            javax.swing.JOptionPane.showMessageDialog(null, "Veuillez choisir une wiimote et un NXT.");
        }
    }//GEN-LAST:event_jbAssocierWiimoteNXTActionPerformed

    private void jbDeconnecterWiimoteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbDeconnecterWiimoteActionPerformed
        // TODO add your handling code here:
        int idWiimote;

        if(listeWiimote.getSelectedItem() != null){
            idWiimote = Integer.parseInt(listeWiimote.getSelectedItem());
            ctrl.deconnecter_Wiimote(idWiimote);
        }
        else{
            javax.swing.JOptionPane.showMessageDialog(null, "Veuillez choisir une wiimote.");
        }
    }//GEN-LAST:event_jbDeconnecterWiimoteActionPerformed

    private void jtfAdresseNXTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfAdresseNXTActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfAdresseNXTActionPerformed

    private void jbAssocierParAdresseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAssocierParAdresseActionPerformed
        // TODO add your handling code here:
        int idWiimote;
        String adresseNXT, nomNXT;
        boolean resAssociation;

        if(listeWiimote.getSelectedItem() != null && !jtfAdresseNXT.getText().equals("") && !jtfNomNXT.getText().equals("")){
            idWiimote = Integer.parseInt(listeWiimote.getSelectedItem());
            nomNXT = jtfNomNXT.getText();
            adresseNXT = jtfAdresseNXT.getText();            

            if(wiimotesAssociees.contains(idWiimote)){
                javax.swing.JOptionPane.showMessageDialog(null, "Cette Wiimote est déjà associée.");
            }else{
                resAssociation = ctrl.associer_Wiimote_NXT_Par_Adresse(idWiimote, nomNXT, adresseNXT);

                if (resAssociation) {
                    listWiimoteNXT.add(idWiimote + " - " + nomNXT);

                    /* Stockage des identifiants de Wiimote et NXT associes */
                    nxtsAssocies.add(nomNXT);
                    wiimotesAssociees.add(""+idWiimote);
                } else {
                    jlblInfoSysteme.setText("Probleme lors de l'association : " + idWiimote + " - " + adresseNXT);
                }
            }
            
        }else{
            javax.swing.JOptionPane.showMessageDialog(null, "Veuillez choisir une Wiimote, saisir un nom de NXT et son adresse.");
        }
       
    }//GEN-LAST:event_jbAssocierParAdresseActionPerformed

    public void enlever_id_wiimote(int id){
        listeWiimote.remove(""+id);
    }

    public void enlever_couple_wiimote_nxt(int wId, String nxtName){
        listWiimoteNXT.remove(wId+" - "+nxtName);
        wiimotesAssociees.remove(""+wId);
        nxtsAssocies.remove(nxtName);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JButton jbAssocierParAdresse;
    private javax.swing.JButton jbAssocierWiimoteNXT;
    private javax.swing.JButton jbConnecterWiimote;
    private javax.swing.JButton jbDeconnecterWiimote;
    private javax.swing.JButton jbListerNXT;
    private javax.swing.JLabel jlblInfoSysteme;
    private javax.swing.JTextField jtfAdresseNXT;
    private javax.swing.JTextField jtfNomNXT;
    private java.awt.List listWiimoteNXT;
    private java.awt.List listeNXT;
    private java.awt.List listeWiimote;
    // End of variables declaration//GEN-END:variables

}
